package android.vstore.fragment;

import java.util.ArrayList;

import com.appvn2014.appstorevn.R;

import DTO.CategoryDTO;
import DTO.ImageDTO;
import DTO.ProductDTO;
import DTO.TopicDTO;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.vstore.activities.MainActivity;
import android.vstore.activities.SceneGroupActivity;
import android.vstore.fragment.PageSectionFragment.CustomAdapterVerticalProduct;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import anroid.vstore.provider.Constant;

public class PageSectionCatergoryFragment extends BaseFragment implements OnItemClickListener {
	
	FragmentActivity activity;
	private View rootView;
	private ListView lv_lstProds;
	public ArrayList<CategoryDTO> _lstItem = new ArrayList<CategoryDTO>();
	public boolean isProductsLoading = false;
	private ProgressBar pb;
	CustomAdapterVerticalCat adapter;
	public boolean isReload;
	
	int id;
	Handler msgHandler;
	private int type;
	
	public static PageSectionCatergoryFragment newInstance(FragmentActivity activity,int id,String name,int type, Handler handler)
	{
		PageSectionCatergoryFragment page = new PageSectionCatergoryFragment();
		page.activity = activity;
		page.isReload = true;
		page.type = type;
		page.name = name;
		page.id = id;
		page.msgHandler = handler;
		return page;
		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_sectionpage, container, false);
		InitGUI();
		Load();
		return rootView;
	}
	public void InitGUI()
	{
		lv_lstProds = (ListView)rootView.findViewById(R.id.lv_prods);
		pb = (ProgressBar) rootView.findViewById(R.id.pb_listview_loading);
		lv_lstProds.setOnItemClickListener(this);
		HideLoaderPending();
	}
	public void Load()
	{
		
		if(isReload )
		{
			(new Loader()).execute();
			isReload = false;
		}
	}
	public class Loader extends AsyncTask<String, Void, ArrayList<CategoryDTO>>{
		@Override
		protected ArrayList<CategoryDTO> doInBackground(String... params) {
			
			ArrayList<CategoryDTO> lstProds = null;
			if(getType() == Constant.TYPE_APPLICATION)
				_lstItem = MainActivity._appcategories;
			else
				_lstItem = MainActivity._gamecategories;
			return lstProds;
		}      
		
		@Override
		protected void onPostExecute(ArrayList<CategoryDTO> lstVipProd) {
//			HideLoaderPending();
			if(this == null && ! PageSectionCatergoryFragment.this.isDetached())
				return;

			adapter = new CustomAdapterVerticalCat(activity	, R.layout.item_list_cats	, _lstItem);
			lv_lstProds.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}

		@Override
		protected void onPreExecute() {
		}

		@Override
		protected void onProgressUpdate(Void... values) {
		}
	}
	
	protected void ShowLoaderPending()
	{		
		pb.setVisibility(View.VISIBLE);
	}
	protected void HideLoaderPending()
	{
		pb.setVisibility(View.INVISIBLE);
	}
	public Handler _messageHandler = new Handler(){
    	@Override
		public void handleMessage(Message msg)
    	{
    	}
	};
	
	public class CustomAdapterVerticalCat extends ArrayAdapter<CategoryDTO>
	{
		ArrayList<CategoryDTO> itemList;
	    public Activity context;
	    public LayoutInflater inflater;
		private final String TAG = "CustomAdapterProduct";
		
	    public CustomAdapterVerticalCat(Activity context, int textViewResource, ArrayList<CategoryDTO> itemList) 
	    {
	        super(context, textViewResource, itemList);

	        this.context = context;
	        this.itemList = itemList; 

	        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    } 

	    @Override 
	    public int getCount() 
	    {
	        return itemList.size();
	    }

	    @Override
	    public CategoryDTO getItem(int position) 
	    {
	        return itemList.get(position);
	    }
	    
	    @Override
	    public long getItemId(int position) 
	    {
	        return 0;
	    }

	    public class ViewHolderProduct
	    {
	        public ImageView imgViewLogo;
	        public TextView txtViewTitle;
	        public TextView txtViewCount;
	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) 
	    {	    	
	    	ViewHolderProduct holder;
	    	if(convertView == null) 
	    	{
	    		holder = new ViewHolderProduct();
	    		convertView = inflater.inflate(R.layout.item_list_cats, null);

	    		holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.iv_itemlistcat_logo);
	    		holder.txtViewTitle = (TextView) convertView.findViewById(R.id.tv_itemlistcat_title);
	    		holder.txtViewCount = (TextView) convertView.findViewById(R.id.tv_itemlistcat_count);
	    		convertView.setTag(holder);
	    	}
	    	else
	    		holder = (ViewHolderProduct)convertView.getTag();

	    	CategoryDTO bean = (CategoryDTO) itemList.get(position);
		    	
	    	if(bean == null) return convertView;
	    	ImageDTO.displayImage( bean.imgURL, holder.imgViewLogo);
	    	
	    	holder.txtViewTitle.setText(bean.name);
	    	holder.txtViewCount.setText(bean.count+"");
	    	holder.txtViewCount.setVisibility(View.INVISIBLE);
	    	return convertView;
	    }
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		int cat = _lstItem.get(arg2).id;
		msgHandler.sendMessage(Message.obtain(msgHandler, SceneGroupActivity.ReLoad, 0, 0, cat));
	}
	private int getType() {
		return type;
	}
	public void setType(int type) {
		if(lv_lstProds == null) return;
		isReload = true;
		this.type = type;
		Load();
	}

}
